package model

import (
	"gowork/dynamicCheckSheet/utils"
	"time"
)

type CheckNode struct {
	CheckNodeId int64 `xorm:"pk autoincr"`
	CheckNodeName string
	NextCheckNodeId int64
	ThePreviousCheckNodeId int64
	RegionId int64
	RoleId int64
	UserList []*User `xorm:"-"`
	CreatedAt time.Time
  	UpdatedAt time.Time
}

func (c *CheckNode)InsertCheckNode(rId int64, cName string, eId int64, ncId int64) (map[string]interface{}, error){

	w := new(utils.Worker)

	cnList := c.QueryCheckNodeList(rId)
	if cnList == nil{
		var cn = CheckNode{
			CheckNodeId  : w.GetId(),
			CheckNodeName: cName,
			NextCheckNodeId: 1,
			ThePreviousCheckNodeId: 1,
			RegionId: rId,
			RoleId: eId,
			CreatedAt: time.Now(),
			UpdatedAt: time.Now(),
		}
		x.Insert(&cn)
	}else {
		var nc CheckNode
		x.Where("check_node_id = ?", ncId).Get(&nc)

		if nc.CheckNodeId == 0{

			firstNode := c.QueryFirstNode(rId)

			var cn = CheckNode{
				CheckNodeId:   w.GetId(),
				CheckNodeName: cName,
				NextCheckNodeId: firstNode.CheckNodeId,
				ThePreviousCheckNodeId: 1,
				RegionId: rId,
				RoleId: eId,
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
			x.Insert(&cn)

			firstNode.ThePreviousCheckNodeId = cn.CheckNodeId
			x.Id(firstNode.CheckNodeId).Update(firstNode)
		}

		if nc.CheckNodeId != 0 && nc.NextCheckNodeId != 1{
			var cn = CheckNode{
				CheckNodeId:   w.GetId(),
				CheckNodeName: cName,
				NextCheckNodeId: nc.NextCheckNodeId,
				ThePreviousCheckNodeId: nc.CheckNodeId,
				RegionId: rId,
				RoleId: eId,
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
			x.Insert(&cn)

			/*
			n1  newn n2
			 */

			newNext := new(CheckNode)
			newNext.ThePreviousCheckNodeId = cn.CheckNodeId
			x.Id(nc.NextCheckNodeId).Update(newNext)

			nc.NextCheckNodeId = cn.CheckNodeId
			x.Id(nc.CheckNodeId).Update(&nc)
		}else if nc.CheckNodeId != 0 && nc.NextCheckNodeId == 1{
			var cn = CheckNode{
				CheckNodeId:            w.GetId(),
				CheckNodeName:          cName,
				NextCheckNodeId:        1,
				ThePreviousCheckNodeId: nc.CheckNodeId,
				RegionId:               rId,
				RoleId:                 eId,
				CreatedAt:              time.Now(),
				UpdatedAt:              time.Now(),
			}
			x.Insert(&cn)

			nc.NextCheckNodeId = cn.CheckNodeId
			x.Id(nc.CheckNodeId).Update(&nc)
		}
	}

	return map[string]interface{}{"message": "成功","code":"200"}, nil
}

func (c *CheckNode)QueryFirstNode(rId int64) *CheckNode{
	cn := new(CheckNode)
	x.Where("region_id = ?", rId).And("the_previous_check_node_id = ?", 1).Get(cn)
	return cn
}

func (c *CheckNode)QueryCheckNodeList(rId int64) []*CheckNode{
	var cnList []*CheckNode
	x.Where("region_id = ?", rId).Desc("created_at").Find(&cnList)
	return cnList
}

func (c *CheckNode)QueryCheckNodeLinkFirst(rId int64) ([]*CheckNode, error){
	var checkNodeList []*CheckNode

	cn := new(CheckNode)
	x.Where("region_id = ?", rId).And("the_previous_check_node_id = ?", 1).Get(cn)
	checkNodeList = append(checkNodeList, cn)

	if cn.NextCheckNodeId != 1 {
		c.QueryCheckNodeLink(cn, &checkNodeList)
	}

	return checkNodeList, nil
}

func (c *CheckNode)QueryCheckNodeLink(ncn *CheckNode, checkNodeList *[]*CheckNode){
	checkNode := new(CheckNode)
	x.Where("check_node_id = ?", ncn.NextCheckNodeId).Get(checkNode)
	*checkNodeList = append(*checkNodeList, checkNode)
	if checkNode.NextCheckNodeId != 1{
		c.QueryCheckNodeLink(checkNode, checkNodeList)
	}
}

func (c *CheckNode)QueryCheckNode(cId int64) *CheckNode{
	cn := new(CheckNode)
	x.Where("check_node_id = ?", cId).Get(cn)
	return cn
}

func (c *CheckNode)DeleteCheckNode(cId int64) (map[string]interface{}, error){
	var p *Paper
	if len(p.QueryPaperList(cId)) > 0{
		return map[string]interface{}{"message": "该节点下还存在未审单","code":"400"}, nil
	}

	var nc CheckNode
	x.Where("check_node_id = ?", cId).Get(&nc)

	/*
	n1 n2 n3
	 */

	if nc.CheckNodeId != 0 && nc.ThePreviousCheckNodeId == 1{

		newNext := new(CheckNode)
		newNext.ThePreviousCheckNodeId = 1
		x.Id(nc.NextCheckNodeId).Update(newNext)

		x.Id(nc.CheckNodeId).Delete(&nc)

		return map[string]interface{}{"message": "成功","code":"200"}, nil
	}

	if nc.CheckNodeId != 0 && nc.NextCheckNodeId != 1 && nc.ThePreviousCheckNodeId != 1{

		newNext := new(CheckNode)
		newNext.ThePreviousCheckNodeId = nc.ThePreviousCheckNodeId
		x.Id(nc.NextCheckNodeId).Update(newNext)

		newPrevious := new(CheckNode)
		newPrevious.NextCheckNodeId = nc.NextCheckNodeId
		x.Id(nc.ThePreviousCheckNodeId).Update(newPrevious)

		x.Id(nc.CheckNodeId).Delete(&nc)

		return map[string]interface{}{"message": "成功","code":"200"}, nil
	}

	newPrevious := new(CheckNode)
	newPrevious.NextCheckNodeId = 1
	x.Id(nc.ThePreviousCheckNodeId).Update(newPrevious)

	x.Id(nc.CheckNodeId).Delete(&nc)

	return map[string]interface{}{"message": "成功","code":"200"}, nil
}


